package com.yxt.qmgf.minesweeper.model;

import javafx.scene.control.Button;
import javafx.scene.control.ContentDisplay;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.text.Font;

public class Cell {
    private final int row;
    private final int col;
    private final Button button;
    private boolean isMine;
    private boolean isRevealed;
    private boolean isFlagged;
    private int adjacentMines;
    private boolean isHighlighted = false;

    private static Image mineImage;
    private static Image flagImage;

    static {
        try {
            // 加载静态图片资源
            mineImage = new Image(Cell.class.getResourceAsStream("/images/mine.png"));
            flagImage = new Image(Cell.class.getResourceAsStream("/images/flag.png"));
        } catch (Exception e) {
            System.err.println("无法加载图片资源: " + e.getMessage());
            // 回退到文本显示
            mineImage = flagImage = null;
        }
    }

    public Cell(int row, int col) {
        this.row = row;
        this.col = col;
        this.button = new Button();
        this.button.setPrefSize(30, 30);
        this.button.getStyleClass().add("cell"); // 只使用.cell样式
        reset(); // 调用reset初始化状态
    }

    public Button getButton() {
        return button;
    }

    public boolean isMine() {
        return isMine;
    }

    public void setMine(boolean mine) {
        isMine = mine;
    }

    public boolean isRevealed() {
        return isRevealed;
    }

    public boolean isFlagged() {
        return isFlagged;
    }

    public int getAdjacentMines() {
        return adjacentMines;
    }

    public void setAdjacentMines(int adjacentMines) {
        this.adjacentMines = adjacentMines;
    }

    public int getRow() {
        return row;
    }

    public int getCol() {
        return col;
    }

    public void reveal() {
        isRevealed = true;
        button.setDisable(true);
        button.getStyleClass().add("revealed");
        // 清除图片显示
        button.setGraphic(null);

        if (isMine) {
            button.getStyleClass().add("mine");
            // 地雷优先使用图片
            if (mineImage != null) {
                ImageView iv = new ImageView(mineImage);
                iv.setFitWidth(26);
                iv.setFitHeight(26);
                button.setGraphic(iv);
                button.setText(""); // 确保文本为空
            } else {
                button.setText("💣");
                button.setGraphic(null);
            }
        } else if (adjacentMines > 0) {
            // 显示数字时移除图片
            button.setText(String.valueOf(adjacentMines));
            button.setGraphic(null);
            button.getStyleClass().add("number-" + adjacentMines);
        } else {
            button.setText("");
            button.setGraphic(null);
        }
    }

    public void toggleFlag() {
        if (!isRevealed) {
            isFlagged = !isFlagged;
            if (isFlagged) {
                if (flagImage != null) {
                    ImageView iv = new ImageView(flagImage);
                    iv.setFitWidth(26);
                    iv.setFitHeight(26);
                    button.setGraphic(iv);
                    button.setText(""); // 确保文本为空
                } else {
                    button.setText("🚩");
                    button.setGraphic(null);
                }
            } else {
                button.setText("");
                button.setGraphic(null);
            }
        }
    }

    // 在Cell类中添加
    public void setHighlighted(boolean highlighted) {
        if (this.isHighlighted == highlighted) return;

        this.isHighlighted = highlighted;
        if (highlighted) {
            button.getStyleClass().add("highlighted-cell");
        } else {
            button.getStyleClass().remove("highlighted-cell");
        }
        System.out.println("格子 ["+row+"]["+col+"] 高亮状态: " + highlighted);
    }
    public void reset() {
        isRevealed = false;
        isFlagged = false;
        isHighlighted = false;
        isMine = false;
        adjacentMines = 0;

        button.getStyleClass().setAll("cell");
        button.setDisable(false);
        button.setText("");

        ImageView iv = new ImageView();
        iv.setFitWidth(24);
        iv.setFitHeight(24);
        button.setGraphic(iv);
        button.setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
    }

}
